package com.lottery.im.module.service.ui;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.bank.UserBankList;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.UserBankListAdapter;
import com.lottery.im.module.service.presenter.BankSelectPresenter;
import com.lottery.im.module.service.view.BankSelectView;
import com.lottery.im.util.SpacesItemDecoration;
import java.util.List;

/**
 * 作者：cyy
 * 时间：2018/9/26。
 */
public class BankSelectFragment extends BaseSwipeFragment<BankSelectPresenter>
    implements BankSelectView {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.recycleView) RecyclerView recycleView;
  private UserBankListAdapter mAdapter;

  private final static int REQUESTCODE=100;
  public final static int RESULTCODE=400;

  @Override protected int getLayoutResId() {
    return R.layout.bank_select;
  }

  @Override protected void initPresenter() {
    presenter = new BankSelectPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    tvTitle.setText("选择银行卡");

    recycleView.setLayoutManager(new LinearLayoutManager(mActivity));
    recycleView.addItemDecoration(new SpacesItemDecoration(10));
    mAdapter = new UserBankListAdapter();
    recycleView.setAdapter(mAdapter);

    presenter.queryUserBankList();

    mAdapter.setOnItemClickListener((position, object) -> {
      Intent intent = new Intent();
      intent.putExtra("userBankList",object);
      setFragmentResult(RESULTCODE,intent);
      popBackStack();
    });
  }

  @OnClick({ R.id.iv_back, R.id.tv_addBank }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_addBank:
        startFragmentForResult(new BankSelectFragment2(),REQUESTCODE);
        break;
    }
  }

  @Override public void loadUserBankListSuccess(List<UserBankList> list) {
    //mAdapter.addList2(list);
    mAdapter.notifyList(list);
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);
    if(requestCode==REQUESTCODE&&resultCode==BankSelectFragment2.RESULTCODE){
      presenter.queryUserBankList();
    }
  }
}
